
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, 2012 and Azure
-- --------------------------------------------------
-- Date Created: 04/15/2014 10:45:38
-- Generated from EDMX file: G:\Users\Ehsan\Desktop\xknight\xKnight.Models\xKnightModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [xKnight];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_AttackWebCrawlingSetting]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Attacks] DROP CONSTRAINT [FK_AttackWebCrawlingSetting];
GO
IF OBJECT_ID(N'[dbo].[FK_AttackCsrfAttack]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CsrfAttacks] DROP CONSTRAINT [FK_AttackCsrfAttack];
GO
IF OBJECT_ID(N'[dbo].[FK_XAttack_Attack]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[XAttacks] DROP CONSTRAINT [FK_XAttack_Attack];
GO
IF OBJECT_ID(N'[dbo].[FK_Host_WebCrawlingSetting]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Hosts] DROP CONSTRAINT [FK_Host_WebCrawlingSetting];
GO
IF OBJECT_ID(N'[dbo].[FK_CsrfAttackCsrfAttackForm]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CsrfAttackForms] DROP CONSTRAINT [FK_CsrfAttackCsrfAttackForm];
GO
IF OBJECT_ID(N'[dbo].[FK_CsrfAttackFormCsrfToken]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CsrfTokens] DROP CONSTRAINT [FK_CsrfAttackFormCsrfToken];
GO
IF OBJECT_ID(N'[dbo].[FK_FormCsrfAttackForm]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CsrfAttackForms] DROP CONSTRAINT [FK_FormCsrfAttackForm];
GO
IF OBJECT_ID(N'[dbo].[FK_WebpageCsrfAttack]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CsrfAttacks] DROP CONSTRAINT [FK_WebpageCsrfAttack];
GO
IF OBJECT_ID(N'[dbo].[FK_FormElementForm]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[FormElements] DROP CONSTRAINT [FK_FormElementForm];
GO
IF OBJECT_ID(N'[dbo].[FK_XAttackParamFormElement]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[XAttackParams] DROP CONSTRAINT [FK_XAttackParamFormElement];
GO
IF OBJECT_ID(N'[dbo].[FK_Form_HostPage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Forms] DROP CONSTRAINT [FK_Form_HostPage];
GO
IF OBJECT_ID(N'[dbo].[FK_XAttack_Form]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[XAttacks] DROP CONSTRAINT [FK_XAttack_Form];
GO
IF OBJECT_ID(N'[dbo].[FK_HostPage_Host]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Webpages] DROP CONSTRAINT [FK_HostPage_Host];
GO
IF OBJECT_ID(N'[dbo].[FK_HostReuqestHostCookie]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[RequestHostCookies] DROP CONSTRAINT [FK_HostReuqestHostCookie];
GO
IF OBJECT_ID(N'[dbo].[FK_HostPage_HostPage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Webpages] DROP CONSTRAINT [FK_HostPage_HostPage];
GO
IF OBJECT_ID(N'[dbo].[FK_XAttackParamXAttack]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[XAttackParams] DROP CONSTRAINT [FK_XAttackParamXAttack];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Attacks]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Attacks];
GO
IF OBJECT_ID(N'[dbo].[WebCrawlingSettings]', 'U') IS NOT NULL
    DROP TABLE [dbo].[WebCrawlingSettings];
GO
IF OBJECT_ID(N'[dbo].[CsrfAttackForms]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CsrfAttackForms];
GO
IF OBJECT_ID(N'[dbo].[CsrfAttacks]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CsrfAttacks];
GO
IF OBJECT_ID(N'[dbo].[CsrfTokens]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CsrfTokens];
GO
IF OBJECT_ID(N'[dbo].[FormElements]', 'U') IS NOT NULL
    DROP TABLE [dbo].[FormElements];
GO
IF OBJECT_ID(N'[dbo].[Forms]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Forms];
GO
IF OBJECT_ID(N'[dbo].[Hosts]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Hosts];
GO
IF OBJECT_ID(N'[dbo].[RequestHostCookies]', 'U') IS NOT NULL
    DROP TABLE [dbo].[RequestHostCookies];
GO
IF OBJECT_ID(N'[dbo].[Webpages]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Webpages];
GO
IF OBJECT_ID(N'[dbo].[XAttackParams]', 'U') IS NOT NULL
    DROP TABLE [dbo].[XAttackParams];
GO
IF OBJECT_ID(N'[dbo].[XAttacks]', 'U') IS NOT NULL
    DROP TABLE [dbo].[XAttacks];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Attacks'
CREATE TABLE [dbo].[Attacks] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [AttackType] int  NOT NULL,
    [StartTime] datetime  NULL,
    [FinishTime] datetime  NULL,
    [HostId] int  NOT NULL,
    [WebCrawlingSettingId] int  NOT NULL
);
GO

-- Creating table 'WebCrawlingSettings'
CREATE TABLE [dbo].[WebCrawlingSettings] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [MaxPages] int  NULL,
    [MaxDepth] int  NULL,
    [MaxTime] bigint  NULL,
    [MaxByte] bigint  NULL,
    [StartTime] datetime  NULL,
    [FinishTime] datetime  NULL,
    [CrawlSubDomains] bit  NOT NULL
);
GO

-- Creating table 'CsrfAttackForms'
CREATE TABLE [dbo].[CsrfAttackForms] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [CsrfAttackId] int  NOT NULL,
    [FormId] int  NOT NULL
);
GO

-- Creating table 'CsrfAttacks'
CREATE TABLE [dbo].[CsrfAttacks] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [AttackId] int  NOT NULL,
    [StartTime] datetime  NULL,
    [FinishTime] datetime  NULL,
    [Succeed] bit  NULL,
    [ResponsePage] nvarchar(max)  NULL,
    [WebpageId] int  NOT NULL
);
GO

-- Creating table 'CsrfTokens'
CREATE TABLE [dbo].[CsrfTokens] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [CsrfAttackFormId] int  NOT NULL
);
GO

-- Creating table 'FormElements'
CREATE TABLE [dbo].[FormElements] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NULL,
    [Value] nvarchar(max)  NULL,
    [Type] nvarchar(max)  NULL,
    [FormId] int  NOT NULL
);
GO

-- Creating table 'Forms'
CREATE TABLE [dbo].[Forms] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [WebpageId] int  NOT NULL,
    [Action] nvarchar(max)  NOT NULL,
    [Method] varchar(50)  NOT NULL
);
GO

-- Creating table 'Hosts'
CREATE TABLE [dbo].[Hosts] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [CrawlId] int  NOT NULL,
    [HostName] nvarchar(2000)  NOT NULL,
    [IndexedPages] int  NOT NULL,
    [BytesDownloaded] bigint  NOT NULL,
    [StartTime] datetime  NULL,
    [FinishTime] datetime  NULL
);
GO

-- Creating table 'RequestHostCookies'
CREATE TABLE [dbo].[RequestHostCookies] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [HostId] int  NOT NULL,
    [Comment] nvarchar(max)  NULL,
    [CommentUri] nvarchar(max)  NULL,
    [Discard] bit  NOT NULL,
    [Domain] nvarchar(max)  NULL,
    [Expired] bit  NOT NULL,
    [Expires] datetime  NULL,
    [Name] nvarchar(max)  NULL,
    [Path] nvarchar(max)  NULL,
    [Port] nvarchar(max)  NULL,
    [Secure] bit  NOT NULL,
    [Value] nvarchar(max)  NULL,
    [Version] int  NULL
);
GO

-- Creating table 'Webpages'
CREATE TABLE [dbo].[Webpages] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [HostId] int  NOT NULL,
    [Url] nvarchar(2000)  NOT NULL,
    [Html] nvarchar(max)  NULL,
    [Depth] int  NOT NULL,
    [DateTime] datetime  NOT NULL,
    [RefererId] int  NULL
);
GO

-- Creating table 'XAttackParams'
CREATE TABLE [dbo].[XAttackParams] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Value] nvarchar(max)  NULL,
    [FormElementId] int  NOT NULL,
    [XAttackId] int  NOT NULL,
    [IsPoison] bit  NOT NULL
);
GO

-- Creating table 'XAttacks'
CREATE TABLE [dbo].[XAttacks] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [AttackId] int  NOT NULL,
    [FormId] int  NOT NULL,
    [AttackContent] nvarchar(max)  NOT NULL,
    [ResponsePage] nvarchar(max)  NULL,
    [StartTime] datetime  NULL,
    [FinishTime] datetime  NULL,
    [Succeed] bit  NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Attacks'
ALTER TABLE [dbo].[Attacks]
ADD CONSTRAINT [PK_Attacks]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'WebCrawlingSettings'
ALTER TABLE [dbo].[WebCrawlingSettings]
ADD CONSTRAINT [PK_WebCrawlingSettings]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'CsrfAttackForms'
ALTER TABLE [dbo].[CsrfAttackForms]
ADD CONSTRAINT [PK_CsrfAttackForms]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'CsrfAttacks'
ALTER TABLE [dbo].[CsrfAttacks]
ADD CONSTRAINT [PK_CsrfAttacks]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'CsrfTokens'
ALTER TABLE [dbo].[CsrfTokens]
ADD CONSTRAINT [PK_CsrfTokens]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'FormElements'
ALTER TABLE [dbo].[FormElements]
ADD CONSTRAINT [PK_FormElements]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Forms'
ALTER TABLE [dbo].[Forms]
ADD CONSTRAINT [PK_Forms]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Hosts'
ALTER TABLE [dbo].[Hosts]
ADD CONSTRAINT [PK_Hosts]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'RequestHostCookies'
ALTER TABLE [dbo].[RequestHostCookies]
ADD CONSTRAINT [PK_RequestHostCookies]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Webpages'
ALTER TABLE [dbo].[Webpages]
ADD CONSTRAINT [PK_Webpages]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'XAttackParams'
ALTER TABLE [dbo].[XAttackParams]
ADD CONSTRAINT [PK_XAttackParams]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'XAttacks'
ALTER TABLE [dbo].[XAttacks]
ADD CONSTRAINT [PK_XAttacks]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [WebCrawlingSettingId] in table 'Attacks'
ALTER TABLE [dbo].[Attacks]
ADD CONSTRAINT [FK_AttackWebCrawlingSetting]
    FOREIGN KEY ([WebCrawlingSettingId])
    REFERENCES [dbo].[WebCrawlingSettings]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_AttackWebCrawlingSetting'
CREATE INDEX [IX_FK_AttackWebCrawlingSetting]
ON [dbo].[Attacks]
    ([WebCrawlingSettingId]);
GO

-- Creating foreign key on [AttackId] in table 'CsrfAttacks'
ALTER TABLE [dbo].[CsrfAttacks]
ADD CONSTRAINT [FK_AttackCsrfAttack]
    FOREIGN KEY ([AttackId])
    REFERENCES [dbo].[Attacks]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_AttackCsrfAttack'
CREATE INDEX [IX_FK_AttackCsrfAttack]
ON [dbo].[CsrfAttacks]
    ([AttackId]);
GO

-- Creating foreign key on [AttackId] in table 'XAttacks'
ALTER TABLE [dbo].[XAttacks]
ADD CONSTRAINT [FK_XAttack_Attack]
    FOREIGN KEY ([AttackId])
    REFERENCES [dbo].[Attacks]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_XAttack_Attack'
CREATE INDEX [IX_FK_XAttack_Attack]
ON [dbo].[XAttacks]
    ([AttackId]);
GO

-- Creating foreign key on [CrawlId] in table 'Hosts'
ALTER TABLE [dbo].[Hosts]
ADD CONSTRAINT [FK_Host_WebCrawlingSetting]
    FOREIGN KEY ([CrawlId])
    REFERENCES [dbo].[WebCrawlingSettings]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Host_WebCrawlingSetting'
CREATE INDEX [IX_FK_Host_WebCrawlingSetting]
ON [dbo].[Hosts]
    ([CrawlId]);
GO

-- Creating foreign key on [CsrfAttackId] in table 'CsrfAttackForms'
ALTER TABLE [dbo].[CsrfAttackForms]
ADD CONSTRAINT [FK_CsrfAttackCsrfAttackForm]
    FOREIGN KEY ([CsrfAttackId])
    REFERENCES [dbo].[CsrfAttacks]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CsrfAttackCsrfAttackForm'
CREATE INDEX [IX_FK_CsrfAttackCsrfAttackForm]
ON [dbo].[CsrfAttackForms]
    ([CsrfAttackId]);
GO

-- Creating foreign key on [CsrfAttackFormId] in table 'CsrfTokens'
ALTER TABLE [dbo].[CsrfTokens]
ADD CONSTRAINT [FK_CsrfAttackFormCsrfToken]
    FOREIGN KEY ([CsrfAttackFormId])
    REFERENCES [dbo].[CsrfAttackForms]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CsrfAttackFormCsrfToken'
CREATE INDEX [IX_FK_CsrfAttackFormCsrfToken]
ON [dbo].[CsrfTokens]
    ([CsrfAttackFormId]);
GO

-- Creating foreign key on [FormId] in table 'CsrfAttackForms'
ALTER TABLE [dbo].[CsrfAttackForms]
ADD CONSTRAINT [FK_FormCsrfAttackForm]
    FOREIGN KEY ([FormId])
    REFERENCES [dbo].[Forms]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_FormCsrfAttackForm'
CREATE INDEX [IX_FK_FormCsrfAttackForm]
ON [dbo].[CsrfAttackForms]
    ([FormId]);
GO

-- Creating foreign key on [WebpageId] in table 'CsrfAttacks'
ALTER TABLE [dbo].[CsrfAttacks]
ADD CONSTRAINT [FK_WebpageCsrfAttack]
    FOREIGN KEY ([WebpageId])
    REFERENCES [dbo].[Webpages]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WebpageCsrfAttack'
CREATE INDEX [IX_FK_WebpageCsrfAttack]
ON [dbo].[CsrfAttacks]
    ([WebpageId]);
GO

-- Creating foreign key on [FormId] in table 'FormElements'
ALTER TABLE [dbo].[FormElements]
ADD CONSTRAINT [FK_FormElementForm]
    FOREIGN KEY ([FormId])
    REFERENCES [dbo].[Forms]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_FormElementForm'
CREATE INDEX [IX_FK_FormElementForm]
ON [dbo].[FormElements]
    ([FormId]);
GO

-- Creating foreign key on [FormElementId] in table 'XAttackParams'
ALTER TABLE [dbo].[XAttackParams]
ADD CONSTRAINT [FK_XAttackParamFormElement]
    FOREIGN KEY ([FormElementId])
    REFERENCES [dbo].[FormElements]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_XAttackParamFormElement'
CREATE INDEX [IX_FK_XAttackParamFormElement]
ON [dbo].[XAttackParams]
    ([FormElementId]);
GO

-- Creating foreign key on [WebpageId] in table 'Forms'
ALTER TABLE [dbo].[Forms]
ADD CONSTRAINT [FK_Form_HostPage]
    FOREIGN KEY ([WebpageId])
    REFERENCES [dbo].[Webpages]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Form_HostPage'
CREATE INDEX [IX_FK_Form_HostPage]
ON [dbo].[Forms]
    ([WebpageId]);
GO

-- Creating foreign key on [FormId] in table 'XAttacks'
ALTER TABLE [dbo].[XAttacks]
ADD CONSTRAINT [FK_XAttack_Form]
    FOREIGN KEY ([FormId])
    REFERENCES [dbo].[Forms]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_XAttack_Form'
CREATE INDEX [IX_FK_XAttack_Form]
ON [dbo].[XAttacks]
    ([FormId]);
GO

-- Creating foreign key on [HostId] in table 'Webpages'
ALTER TABLE [dbo].[Webpages]
ADD CONSTRAINT [FK_HostPage_Host]
    FOREIGN KEY ([HostId])
    REFERENCES [dbo].[Hosts]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_HostPage_Host'
CREATE INDEX [IX_FK_HostPage_Host]
ON [dbo].[Webpages]
    ([HostId]);
GO

-- Creating foreign key on [HostId] in table 'RequestHostCookies'
ALTER TABLE [dbo].[RequestHostCookies]
ADD CONSTRAINT [FK_HostReuqestHostCookie]
    FOREIGN KEY ([HostId])
    REFERENCES [dbo].[Hosts]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_HostReuqestHostCookie'
CREATE INDEX [IX_FK_HostReuqestHostCookie]
ON [dbo].[RequestHostCookies]
    ([HostId]);
GO

-- Creating foreign key on [RefererId] in table 'Webpages'
ALTER TABLE [dbo].[Webpages]
ADD CONSTRAINT [FK_HostPage_HostPage]
    FOREIGN KEY ([RefererId])
    REFERENCES [dbo].[Webpages]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_HostPage_HostPage'
CREATE INDEX [IX_FK_HostPage_HostPage]
ON [dbo].[Webpages]
    ([RefererId]);
GO

-- Creating foreign key on [XAttackId] in table 'XAttackParams'
ALTER TABLE [dbo].[XAttackParams]
ADD CONSTRAINT [FK_XAttackParamXAttack]
    FOREIGN KEY ([XAttackId])
    REFERENCES [dbo].[XAttacks]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_XAttackParamXAttack'
CREATE INDEX [IX_FK_XAttackParamXAttack]
ON [dbo].[XAttackParams]
    ([XAttackId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------